package com.fone.player.activity.personal;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fone.player.R;
import com.fone.player.activity.BaseFragment;
import com.fone.player.activity.local.LocalActivity;
import com.fone.player.activity.local.SortListUtil;
import com.fone.player.activity.main.MainTabActivity;
import com.fone.player.activity.search.SearchActivity;
import com.fone.player.bean.RecordData;
import com.fone.player.billing.IFonePayManager;
import com.fone.player.billing.impl.FonePayManager;
import com.fone.player.billing.util.FoneConstants;
import com.fone.player.cache.ImageDownloadModule;
import com.fone.player.client.Callback;
import com.fone.player.client.CollectionlsRst;
import com.fone.player.client.Error;
import com.fone.player.client.Reporter;
import com.fone.player.client.Request;
import com.fone.player.client.VbRst;
import com.fone.player.constant.FoneConstant;
import com.fone.player.entity.OfflineCache;
import com.fone.player.entity.PlayRecord;
import com.fone.player.online.activity.EveryoneWatchActivity;
import com.fone.player.sns.UserInfoManager;
import com.fone.player.storage.SharedPreferenceModule;
import com.fone.player.storage.StorageModule;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import de.greenrobot.event.EventBus;

/**
 * 播控界面的一级菜单,包括通知,收藏,本地,缓存,设置,登录,网络播放,用户信息模块入口
 * 
 * @author JasonZue
 * @since 2014-4-21
 */
public class PersonalFragment extends BaseFragment implements OnClickListener {

	private static final String TAG = "PersonalFragment";

	private Context mContext;

	private ImageView mNotificationIcon, mSettingIcon;
	private ImageView mHeadImageTv;
	private RelativeLayout mCollectionRl, mLocalRl, mCacheRl, mNetPlayRl,
			mHeadIconRl;
	private Button mChargerBtn, mVipBtn;
	private View mView;
	private IFonePayManager mFonePayManger;
	private TextView mTitleText, mLocalVideoCount, mCollectionCount,
			mCacheCount, mUsernameTv, mVBLeftTv, mHeadLoginTv;
	private TextView mVipIconTv1, mVipIconTv2, mVipIconTv3;
	private CheckBox mHideBox;
	private ImageView mCommonTopSearchIcon, mHotIcon;
	private ImageView mCollectionNewCountTV, mCacheNewCountTV;

	private CollectionCallback collectionCallback;

	private ImageView iv_new_notification_icon;

	private TextView net_play_url;

	public static PersonalFragment newInstance() {
		PersonalFragment f = new PersonalFragment();
		return f;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (mView == null) {
			mView = inflater.inflate(R.layout.activity_personal, container,
					false);
		}
		ViewGroup parent = (ViewGroup) mView.getParent();
		if (parent != null) {
			parent.removeView(mView);
		}
		initView();
		initDate();
		FoneUtil.isNetOkWithToast(mContext);
		return mView;
	}

	/**
	 * 初始化数据
	 * 
	 * @return void
	 * @throws
	 */
	private void initDate() {
		initUserInfo();
		collectionCallback = new CollectionCallback();
		getCollectionCount();
	}

	/**
	 * 获取已缓存数量
	 * 
	 * @return void
	 * @throws
	 */
	private void getCacheCount() {

		int currentCount = StorageModule.getInstance()
				.getOfflineCacheFileCount();
		int newCount = SharedPreferenceModule.getInstance().getInt(
				FoneConstant.LAST_CACHE_COUNT_SP);
		L.v("JasonZue", "当前数量:" + currentCount + "历史数量:" + newCount);
		if (currentCount > newCount) {
			mCacheNewCountTV.setVisibility(View.VISIBLE);
		} else {
			mCacheNewCountTV.setVisibility(View.GONE);
		}

		mCacheCount.setText(currentCount + "个");

	}

	/**
	 * 获取收藏数量
	 * 
	 * @return void
	 * @throws
	 */

	private void getCollectionCount() {
		int favouriteCount = StorageModule.getInstance().getFavouriteCount(
				OfflineCache.CACHE_FAVOURITE_SYNCHRONZIE_CLIENT_ALL);
		L.v(TAG, "getCollectionCount", "favouriteCount=" + favouriteCount);
		mCollectionCount.setText(favouriteCount + "个");

	}

	/**
	 * 初始化用户显示信息
	 */
	private void initUserInfo() {
		if (UserInfoManager.isLogin()) {
			if (UserInfoManager.isVip()) {
				changeIconToVip();
			} else {
				changeIconToUNVip();
			}
			mHeadLoginTv.setText("个人资料");
			mUsernameTv.setText(UserInfoManager.getUserNickName());
			// TODO 请求vb
			if (FoneUtil.isNetOk(mContext)) {
				Request.getInstance().vb(new VbCallBack());
			}
			String userHeadUrl = UserInfoManager.getUserHeadPic();
			L.v(TAG, "initUserInfo", " userHeadUrl : " + userHeadUrl);
			if (!TextUtils.isEmpty(userHeadUrl)
					&& userHeadUrl.startsWith("http://")) {

				DisplayImageOptions mOptions = new DisplayImageOptions.Builder()
						.showImageOnLoading(R.drawable.my_head_default) // 设置图片在下载期间显示的图片
						.showImageForEmptyUri(R.drawable.my_head_default)// 设置图片Uri为空或是错误的时候显示的图片
						.showImageOnFail(R.drawable.my_head_default) // 设置图片加载/解码过程中错误时候显示的图片
						.cacheInMemory(true)// 设置下载的图片是否缓存在内存中
						.cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
						.considerExifParams(true) // 是否考虑JPEG图像EXIF参数（旋转，翻转）
						.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)// 设置图片以如何的编码方式显示
						.bitmapConfig(Bitmap.Config.RGB_565)// 设置图片的解码类型//
						.resetViewBeforeLoading(true)// 设置图片在下载前是否重置，复位
						.build();// 构建完成

				ImageDownloadModule.getInstance().download(
						UserInfoManager.getUserHeadPic(), mHeadImageTv,
						mOptions);
			}

		} else {
			mHeadLoginTv.setText("点击登录");
			changeIconToUNVip();
			mUsernameTv.setText("未登录");
			mHeadImageTv.setImageResource(R.drawable.my_head_default);
			mVBLeftTv.setText("余额0v币");
		}

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = getActivity().getApplicationContext();
		EventBus.getDefault().register(this);
	}

	private void initView() {
		L.v(TAG, "initView", "----initView: start----");
		mHideBox = (CheckBox) mView.findViewById(R.id.hideTab);
		mLocalVideoCount = (TextView) mView
				.findViewById(R.id.local_video_count_tv);
		mTitleText = (TextView) mView.findViewById(R.id.titleText);
		mHeadLoginTv = (TextView) mView
				.findViewById(R.id.personal_control_head_login_tv);
		mCollectionCount = (TextView) mView
				.findViewById(R.id.collection_count_tv);
		mCacheCount = (TextView) mView.findViewById(R.id.cache_count_tv);
		mUsernameTv = (TextView) mView.findViewById(R.id.personal_user_name_tv);
		mVBLeftTv = (TextView) mView.findViewById(R.id.personal_vb_account_tv);

		mVipIconTv1 = (TextView) mView.findViewById(R.id.personal_vip_icon_tv1);
		mVipIconTv2 = (TextView) mView.findViewById(R.id.personal_vip_icon_tv2);
		mVipIconTv3 = (TextView) mView.findViewById(R.id.personal_vip_icon_tv3);

		net_play_url = (TextView) mView.findViewById(R.id.net_play_url);

		mCollectionNewCountTV = (ImageView) mView
				.findViewById(R.id.collection_count_new_tv);
		mCacheNewCountTV = (ImageView) mView
				.findViewById(R.id.cache_count_new_tv);

		mHotIcon = (ImageView) mView.findViewById(R.id.common_top_hot_icon_iv);
		mCommonTopSearchIcon = (ImageView) mView
				.findViewById(R.id.common_top_search_icon_iv);
		mTitleText.setText("我的视频");
		mHideBox.setVisibility(View.GONE);
		mHotIcon.setOnClickListener(this);
		mCommonTopSearchIcon.setOnClickListener(this);

		mHeadImageTv = (ImageView) mView
				.findViewById(R.id.personal_control_header_image_tv);

		mNotificationIcon = (ImageView) mView
				.findViewById(R.id.notification_icon_iv);
		mNotificationIcon.setOnClickListener(this);
		iv_new_notification_icon = (ImageView) mView
				.findViewById(R.id.iv_new_notification_icon);
		mSettingIcon = (ImageView) mView.findViewById(R.id.setting_icon_iv);
		mSettingIcon.setOnClickListener(this);

		mCollectionRl = (RelativeLayout) mView
				.findViewById(R.id.personal_collection_rl);
		mLocalRl = (RelativeLayout) mView.findViewById(R.id.personal_local_rl);
		mCacheRl = (RelativeLayout) mView.findViewById(R.id.personal_cache_rl);
		mHeadIconRl = (RelativeLayout) mView
				.findViewById(R.id.personal_control_header_rl);
		mNetPlayRl = (RelativeLayout) mView
				.findViewById(R.id.personal_net_play_rl);
		mHeadIconRl.setOnClickListener(this);
		mChargerBtn = (Button) mView.findViewById(R.id.personal_charge_bt);
		mVipBtn = (Button) mView.findViewById(R.id.personal_join_vip_bt);
		mVipBtn.setOnClickListener(this);
		mChargerBtn.setOnClickListener(this);
		mCollectionRl.setOnClickListener(this);
		mLocalRl.setOnClickListener(this);
		mCacheRl.setOnClickListener(this);
		mNetPlayRl.setOnClickListener(this);
		L.v(TAG, "initView", "----initView: success----");
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {

		case R.id.common_top_search_icon_iv:
			// 搜索按钮
			intent = new Intent(getActivity(), SearchActivity.class);
			startActivity(intent);
			break;
		case R.id.common_top_hot_icon_iv:
			// 大家都在看
			intent = new Intent(getActivity(), EveryoneWatchActivity.class);
			startActivity(intent);
			break;
		case R.id.personal_control_header_rl:
			// 个人信息
			if (UserInfoManager.isLogin()) {
				intent = new Intent(getActivity(), UserInfoActivity.class);
				startActivity(intent);
			} else {
				FoneUtil.toLoginPage(getActivity(), "1");// 1 点击头像进入的登录页
			}
			break;
		case R.id.notification_icon_iv:
			// 通知
			intent = new Intent(getActivity(), NotificationActivity.class);
			startActivity(intent);
			break;
		case R.id.setting_icon_iv:
			// 设置
			intent = new Intent(getActivity(), SettingActivity.class);
			startActivity(intent);
			break;
		case R.id.personal_net_play_rl:
			// 网络播放
			intent = new Intent(getActivity(), OnlinePlayActivity.class);
			startActivity(intent);
			break;
		case R.id.personal_collection_rl:
			// 个人收藏
			Reporter.logEvent(Reporter.EventId.CLICK_MY_COLLECT_BTN);

			if (FoneUtil.isNetOkWithToast(mContext)) {
				intent = new Intent(getActivity(), NetCollectionActivity.class);
				startActivity(intent);
			}
			break;
		case R.id.personal_cache_rl:
			// 缓存视频
			Reporter.logEvent(Reporter.EventId.CLICK_MY_CACHE_BTN);
			intent = new Intent(getActivity(), CacheActivity.class);
			startActivity(intent);
			break;
		case R.id.personal_local_rl:
			// 本地视频
			Reporter.logEvent(Reporter.EventId.CLICK_LOCAL_BTN);
			intent = new Intent(getActivity(), LocalActivity.class);
			startActivity(intent);
			break;
		case R.id.personal_charge_bt:
			// 充值
			Reporter.logEvent(Reporter.EventId.CLICK_RECHARGE_BTN);
			mFonePayManger = FonePayManager.getInstance();
			mFonePayManger.startPay(FoneConstants.DO_CHARGE_ACTION,
					FoneConstants.FROM_CHARGE_VB, "", "");
			// if (UserInfoManager.isLogin()) {
			// L.i(TAG, "charge_login_correct", "to thirdPayActivity");
			// mFonePayManger = FonePayManager.getInstance();
			// mFonePayManger.startThirdPay(FoneConstants.DO_CHARGE_ACTION,
			// FoneConstants.FROM_CHARGE_VB);
			// } else {
			// Toast.makeText(mContext, R.string.personal_login_tip,
			// Toast.LENGTH_LONG).show();
			// L.i(TAG, "charge_unLogin:", UserInfoManager.isLogin());
			// intent = new Intent(mContext, LoginActivity.class);
			// startActivity(intent);
			// }
			break;
		case R.id.personal_join_vip_bt:
			// 购买VIP
			if (!UserInfoManager.isVip()) {
				Reporter.logEvent(Reporter.EventId.CLICK_VIP_BTN);
				mFonePayManger = FonePayManager.getInstance();
				mFonePayManger.startPay(FoneConstants.DO_VIP_ACTION,
						FoneConstants.FROM_BE_VIP,
						FoneConstants.FEE_FROM_BE_VIP, "");
			}
			// mFonePayManger.testStarPay(FoneConstants.DO_VIP_ACTION,
			// FoneConstants.FROM_OTHER);
			// mFonePayManger.startPay(FoneConstants.DO_VIP_ACTION,
			// FoneConstants.FROM_OTHER);
			// mFonePayManger.testStarPay(FoneConstants.DO_VIP_ACTION,
			// FoneConstants.FROM_OTHER);
			break;
		default:
			break;
		}

	}

	public void onEventMainThread(Message msg) {
		switch (msg.what) {
		case StorageModule.MSG_ACTION_SCANNER_FINISHED:
			StorageModule.getInstance().getVideoFileList(
					StorageModule.LIST_TYPE_FILE_NORMAL_VIDEO);
			int count = StorageModule.getInstance().getVideoCount();
			L.i(TAG, "media count:" + count);
			mLocalVideoCount.setText(count + "个");
			break;
		default:
			break;
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		initUserInfo();

		L.v(TAG, "onResume", "---- start scaning available storage----");
		if (mLocalVideoCount != null) {
			int count = StorageModule.getInstance().getVideoCount();
			mLocalVideoCount.setText(count + "个");
		}
		StorageModule.getInstance().scanningMediaStore();
		ArrayList<PlayRecord> NetPlayRecords = StorageModule.getInstance()
				.getPlayRecordList(PlayRecord.RECORD_WEB_NETWORK);
		if (NetPlayRecords != null && NetPlayRecords.size() > 0) {
			L.i(TAG, "onResume", "NetPlayRecords:" + NetPlayRecords);
			SortListUtil.sortHistoryRecord(NetPlayRecords);
			String playRecordName = NetPlayRecords.get(0).getPlayRecordName();
			L.i(TAG, "onResume", "playRecordName:" + playRecordName);
			net_play_url.setText(playRecordName);
		} else {
			net_play_url.setText("");
		}
		// TODO 添加同步限制条件
		if (UserInfoManager.isLogin()) {
			L.v("JasonZue", "执行了同步");
			doSynchronizing();
		} else {
			getCollectionCount();
		}
		getCacheCount();
		getNewCollectionCount();
		// 判断是否展示新通知icon
		boolean hasNewNotification = SharedPreferenceModule.getInstance()
				.getBoolean(MainTabActivity.NEWNOTIFICATION, false);
		showNewNotificationIcon(hasNewNotification);
	}

	/**
	 * 获取更新的剧集数量
	 */
	private void getNewCollectionCount() {
		SharedPreferences mSp = mContext.getSharedPreferences(
				FoneConstant.FONE_SP_NAME_SP, Activity.MODE_PRIVATE);
		int collectionCount = mSp.getInt(
				FoneConstant.COLLETION_TV_UPDATE_COUNT, 0);
		if (collectionCount != 0) {
			mCollectionNewCountTV.setVisibility(View.VISIBLE);
		} else {
			mCollectionNewCountTV.setVisibility(View.GONE);
		}

	}

	/**
	 * 更换VIP图标
	 * 
	 * @return void
	 * @throws
	 */
	private void changeIconToUNVip() {

		Drawable drawable1 = mContext.getResources().getDrawable(
				R.drawable.my_vip_free_small);
		Drawable drawable2 = mContext.getResources().getDrawable(
				R.drawable.my_vip_present_small);
		Drawable drawable3 = mContext.getResources().getDrawable(
				R.drawable.my_vip_ad_small);
		Drawable drawable4 = mContext.getResources().getDrawable(
				R.drawable.my_icon_vip_default);

		drawable1.setBounds(0, 0, drawable1.getMinimumWidth(),
				drawable1.getMinimumHeight());
		drawable2.setBounds(0, 0, drawable2.getMinimumWidth(),
				drawable2.getMinimumHeight());
		drawable3.setBounds(0, 0, drawable3.getMinimumWidth(),
				drawable3.getMinimumHeight());
		drawable4.setBounds(0, 0, drawable4.getMinimumWidth(),
				drawable4.getMinimumHeight());

		mVipIconTv1.setCompoundDrawables(null, drawable1, null, null);
		mVipIconTv2.setCompoundDrawables(null, drawable2, null, null);
		mVipIconTv3.setCompoundDrawables(null, drawable3, null, null);
		mUsernameTv.setCompoundDrawables(null, null, drawable4, null);

		mVipBtn.setVisibility(View.VISIBLE);

	}

	private void changeIconToVip() {
		Drawable drawable1 = mContext.getResources().getDrawable(
				R.drawable.my_vip_free_small_click);
		Drawable drawable2 = mContext.getResources().getDrawable(
				R.drawable.my_vip_present_small_click);
		Drawable drawable3 = mContext.getResources().getDrawable(
				R.drawable.my_vip_ad_small_click);
		Drawable drawable4 = mContext.getResources().getDrawable(
				R.drawable.my_icon_vip);
		drawable1.setBounds(0, 0, drawable1.getMinimumWidth(),
				drawable1.getMinimumHeight());
		drawable2.setBounds(0, 0, drawable2.getMinimumWidth(),
				drawable2.getMinimumHeight());
		drawable3.setBounds(0, 0, drawable3.getMinimumWidth(),
				drawable3.getMinimumHeight());
		drawable4.setBounds(0, 0, drawable4.getMinimumWidth(),
				drawable4.getMinimumHeight());

		mVipIconTv1.setCompoundDrawables(null, drawable1, null, null);
		mVipIconTv2.setCompoundDrawables(null, drawable2, null, null);
		mVipIconTv3.setCompoundDrawables(null, drawable3, null, null);
		mUsernameTv.setCompoundDrawables(null, null, drawable4, null);

		mVipBtn.setVisibility(View.INVISIBLE);
	}

	// 修改通知icon的标记
	public void showNewNotificationIcon(boolean isShow) {
		if (isShow) {
			iv_new_notification_icon.setVisibility(View.VISIBLE);
		} else {
			iv_new_notification_icon.setVisibility(View.GONE);
		}
	}

	/**
	 * 请求vb回调
	 * 
	 * @author user
	 * 
	 */
	private class VbCallBack implements Callback<VbRst> {

		@Override
		public void onSuccess(VbRst t) {
			L.v(TAG, "VbCallBack onSuccess", t.toString());
			if (t.result != 0) {
				if (t.error != null) {
					L.v(TAG, "VbCallBack onSuccess", "errorcode : "
							+ t.error.errorcode + " errormsg : "
							+ t.error.errormsg);
					return;
				}
			}
			if (!TextUtils.isEmpty(t.vb)) {
				// UserInfoManager.saveUserVbCount(t.vb);//更新本地用户vb数量
				mVBLeftTv.setText("余额" + t.vb + "v币");
			}
		}

		@Override
		public void onFailure(Error error) {
			L.v(TAG,
					"VbCallBack onFailure",
					"isNetworkError : " + error.isNetworkError() + " status : "
							+ error.getStatus() + " reason : "
							+ error.getReason());
		}
	}

	/**
	 * 同步收藏数据
	 * 
	 * @return void
	 * @throws
	 */

	private void doSynchronizing() {
		// 登录成功 同步收藏信息
		ArrayList<OfflineCache> unsynchronized = StorageModule.getInstance()
				.getFavouriteList(
						OfflineCache.CACHE_FAVOURITE_SYNCHRONZIE_ALL_WAIT);
		L.v("JasonZue", "未同步的数据:" + unsynchronized.toString());
		List<RecordData> updateInfo = new ArrayList<RecordData>();
		for (int i = unsynchronized.size(); i > 0; i--) {
			RecordData recordData = new RecordData();
			recordData.ccid = unsynchronized.get(i - 1).getCacheCCID();
			recordData.cid = unsynchronized.get(i - 1).getCacheCID();
			recordData.videoid = unsynchronized.get(i - 1).getCacheVideoId();
			recordData.type = unsynchronized.get(i - 1)
					.getCacheSynchronzieType();
			recordData.clid = unsynchronized.get(i - 1).getCacheCLID();
			recordData.ifp = unsynchronized.get(i - 1).getCacheReportPage();
			L.v("JasonZue", "我的视频页,收藏同步数据上报页面:"
					+ unsynchronized.get(i - 1).getCacheReportPage());
			recordData.updatetime = unsynchronized.get(i - 1)
					.getCacheCreateTime();
			updateInfo.add(recordData);
			L.v("JasonZue",
					"我的视频--> 同步数据上传的videoId:"
							+ unsynchronized.get(i - 1).getCacheVideoId()
							+ " 同步数据上传的时间:"
							+ unsynchronized.get(i - 1).getCacheCreateTime());
		}

		if (updateInfo.size() > 0) {
			Request.getInstance().collectionls(1, 1, updateInfo,
					collectionCallback);
		} else {
			Request.getInstance().collectionls(1, 1, null, collectionCallback);
			L.e(TAG, "doSynchronizing", "没有需要同步的数据!! no Synchronizing data");
		}

	}

	/**
	 * 将同步后的文件状态记录为已同步
	 * 
	 * @return void
	 * @throws
	 */
	private void changeSynchroizeStatus2Already() {
		ArrayList<OfflineCache> unsynchronized = StorageModule.getInstance()
				.getFavouriteList(
						OfflineCache.CACHE_FAVOURITE_SYNCHRONZIE_ALL_WAIT);
		L.v("JasonZue", "before synchronize unsynchronized count:"
				+ unsynchronized.size());
		for (int i = 0; i < unsynchronized.size(); i++) {
			L.v("JasonZue", "before synchronize unsynchronized count info:"
					+ unsynchronized.get(i).getCacheName()
					+ " unsynchronized type:"
					+ unsynchronized.get(i).getCacheSynchronzieType());
			unsynchronized.get(i).setCacheSynchronzieType(
					OfflineCache.CACHE_FAVOURITE_SYNCHRONZIE_ADD_ALREADY);
			L.v("JasonZue", "after synchronize unsynchronized count info:"
					+ unsynchronized.get(i).getCacheName()
					+ " unsynchronized type:"
					+ unsynchronized.get(i).getCacheSynchronzieType());
		}
		StorageModule.getInstance().updateFavouriteSynchronzieType(
				unsynchronized);
		L.v("JasonZue",
				"after synchronize unsynchronized count:"
						+ StorageModule
								.getInstance()
								.getFavouriteList(
										OfflineCache.CACHE_FAVOURITE_SYNCHRONZIE_ALL_WAIT)
								.size());
	}

	private class CollectionCallback implements Callback<CollectionlsRst> {

		@Override
		public void onSuccess(CollectionlsRst t) {

			L.v(TAG, "CollectionCallback onSuccess", "收藏:" + t.toString());

			List<OfflineCache> collectionDataNew = new ArrayList<OfflineCache>();

			if (t.result == 0) {
				// 获取cnt列表数据
				if (null != t.cnts) {
					if (null != t.cnts.cntList) {
						L.v(TAG, "CollectionCallback onSuccess", "收藏列表非空"
								+ t.cnts.cntList.toString());
						for (int i = 0; i < t.cnts.cntList.size(); i++) {
							OfflineCache itemBean = new OfflineCache();
							itemBean.setCacheContentType(t.cnts.cntList.get(i).type);
							itemBean.setCacheName(t.cnts.cntList.get(i).name);
							itemBean.setCacheDetailUrl(t.cnts.cntList.get(i).durl);
							itemBean.setCacheXYZPlayUrl(t.cnts.cntList.get(i).url);
							itemBean.setCacheEpisodeNum(t.cnts.cntList.get(i).season);
							itemBean.setCacheImageUrl(t.shost
									+ FoneUtil.formatUrl(t.cnts.cntList.get(i).pic));
							itemBean.setCacheEpisodeUpdateInfo(t.cnts.cntList
									.get(i).updateinfo);
							itemBean.setCacheIsDownloadable(t.cnts.cntList
									.get(i).btndown == 0 ? false : true);
							itemBean.setCacheDownloadState(OfflineCache.CACHE_STATE_NOT_DOWNLOAD);
							itemBean.setCacheIsDelete(false);
							itemBean.setCacheIsDownlaod(false);
							itemBean.setCacheSynchronzieType(OfflineCache.CACHE_FAVOURITE_SYNCHRONZIE_ADD_ALREADY);
							itemBean.setCacheIsVip(t.cnts.cntList.get(i).vip == 1 ? true
									: false);
							NetCollectionActivity.formatIDS(itemBean,
									t.cnts.cntList.get(i).rurl);
							if (t.cnts.cntList.get(i).type == 2
									|| t.cnts.cntList.get(i).type == 3
									|| t.cnts.cntList.get(i).type == 4) {
								itemBean.setCacheDramaUrl(t.cnts.cntList.get(i).durl);
							} else {
								itemBean.setCacheDramaUrl(null);
							}

							collectionDataNew.add(itemBean);
						}
					}
				}

				// 同步收藏列表到数据库并合并内容
				StorageModule.getInstance().addFavouriteList(collectionDataNew);
				L.v("JasonZue",
						"添加到收藏列表以后 表中的数据:"
								+ StorageModule
										.getInstance()
										.getFavouriteList(
												OfflineCache.CACHE_FAVOURITE_SYNCHRONZIE_CLIENT_ALL)
										.toString());
				changeSynchroizeStatus2Already();
				getCollectionCount();

			} else {
				L.v(TAG, "CollectionCallback onSuccess",
						"收藏collectionls:服务器报文返回错误");
			}

		}

		@Override
		public void onFailure(Error error) {
			L.v(TAG, "CollectionCallback onFailure", "isNetworkError : "
					+ error.isNetworkError() + " status : " + error.getStatus()
					+ " reason : " + error.getReason());
		}

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}
}
